﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test
{
    public partial class frmEditor : Form
    {
        public frmEditor()
        {
            InitializeComponent();
            init();

            _value = "";
            _key = txtKey.Text = "key";
            _tag = txtTag.Text = "default";
            SetTag = true;

            txtKey.SelectAll();
        }

        public frmEditor(string tag,string key, string value)
        {
            InitializeComponent();
            init();

            if (key == null) key = "";
            if (value == null) value = "";
            else if (value.IndexOf("\r\n") > 0)
            {
                this.Height = bigheight + 21;
                frmEditor_Resize(null, null);
            }

            _key = txtKey.Text = key;
            _value = txtValue.Text = value;
            _tag = txtTag.Text = tag;

            if (value != "")
                txtValue.SelectAll();
            else
                txtKey.Focus();

            if (tag == "")
            {
                SetTag = true;
                txtTag.SelectAll();
            }

        }


        public TextBox txtTag;
        void init()
        {
            minpix = txtKey.Top;
            maxpix = lnkSet.Left - lnkCancel.Right;
            nomalheight = this.Height;
            bigheight = nomalheight + minpix + txtKey.Height;
            topheight = Height - lnkCancel.Bottom - maxpix;
            leftwidth = Width - txtValue.Right - minpix;
            this.MinimumSize = new Size(minwidth, nomalheight);


            txtTag = new TextBox();
            txtTag.Name = "Tag";
            txtTag.Visible = false;
            this.Controls.Add(txtTag);

            txtTag.KeyDown += new KeyEventHandler(txtValue_KeyDown);
            txtKey.KeyDown += new KeyEventHandler(txtValue_KeyDown);
            txtTag.DoubleClick += new EventHandler(txtKey_DoubleClick);
            txtKey.DoubleClick += new EventHandler(txtKey_DoubleClick);

            setKeyProc(this);
            
            frmEditor_Resize(null, null);
        }
        void setKeyProc(Control ctrl)
        {
            ctrl.KeyDown += new KeyEventHandler(frmEditor_KeyDown);
            foreach (Control i in ctrl.Controls)
                setKeyProc(i);
        }

        bool SetTag
        {
            set
            {
                if (value == true)
                {
                    txtTag.Visible = true;
                    txtKey.Visible = false;
                    txtTag.SelectAll();
                    label1.Text = "Set Item Tag...DoubleClick to set key";
                }
                else
                {
                    txtTag.Visible = false;
                    txtKey.Visible = true;
                    txtKey.SelectAll();
                    label1.Text = "Set Item Value...DoubleClick to set Tag";
                }
            }
            get
            {
                return txtTag.Visible;
            }
        }

        int minpix, maxpix;
        int nomalheight, bigheight;
        int minwidth = 250;
        int minkeywidth = 80;
        int topheight,leftwidth;
        string _key = "", _value = "",_tag = "";

        private void frmEditor_Resize(object sender, EventArgs e)
        {
            this.SuspendLayout();
            if (this.Height <= bigheight)
            {
                txtValue.Multiline = false;
                this.Height = nomalheight;
                if (Width < minwidth) Width = minwidth;
                txtValue.Top = minpix;
                txtKey.Width = Math.Max((int)((float)this.Width * 0.20f), minkeywidth);
                txtValue.Left = txtKey.Right + minpix;
                txtValue.Width = (Width - leftwidth) - minpix * 3 - txtKey.Width;

                lnkSet.Left = (Width - leftwidth) - maxpix - lnkSet.Width;
                lnkCancel.Left = lnkSet.Left - maxpix - lnkCancel.Width;
                lnkRemove.Left = lnkCancel.Left - maxpix - lnkRemove.Width;
                lnkSet.Top = lnkCancel.Top = lnkRemove.Top = (Height - topheight) - maxpix - lnkSet.Height;
                label1.Top = (Height - topheight) - maxpix - label1.Height;
            }
            else
            {
                txtValue.Multiline = true;
                txtValue.Width = txtKey.Width = (Width - leftwidth) - minpix * 2;
                txtValue.Left = minpix;
                txtValue.Top = txtKey.Bottom + minpix;

                lnkSet.Left = (Width - leftwidth) - maxpix - lnkSet.Width;
                lnkCancel.Left = lnkSet.Left - maxpix - lnkCancel.Width;
                lnkRemove.Left = lnkCancel.Left - maxpix - lnkRemove.Width;
                lnkSet.Top = lnkCancel.Top = lnkRemove.Top = (Height - topheight) - maxpix - lnkSet.Height;

                txtValue.Height = lnkSet.Top - txtValue.Top - maxpix;
                label1.Top = (Height - topheight) - maxpix - label1.Height;
            }

            txtTag.Location = txtKey.Location;
            txtTag.Size = txtKey.Size;

            this.ResumeLayout();
        }

        public string Key
        {
            get{ return txtKey.Text;}
        }
        public string Value
        {
            get { return txtValue.Text; }
        }
        public string UserTag
        {
            get { return txtTag.Text; }
        }

        private void lnkSet_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (txtKey.Text != "")
                this.DialogResult = DialogResult.OK;
            else
            {
                if (MessageBox.Show("Key is empty, are you sure to remove this item?", "Remove Item", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                    this.DialogResult = DialogResult.No;
                else
                {
                    txtKey.Focus();
                    return;
                }
            }
            this.Close();
        }

        private void lnkCancel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.txtKey.Text = _key;
            this.txtValue.Text = _value;
            this.txtTag.Text = _tag;
            this.Close();
        }

        private void lnkRemove_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void txtValue_KeyDown(object sender, KeyEventArgs e)
        {
            //回车 未全选
            TextBox txtbox = sender as TextBox;
            if (e.KeyCode == Keys.Enter)
                if ((txtbox.Multiline == false && txtbox.SelectionLength != txtbox.Text.Length) || (e.Modifiers == Keys.Control))
                    lnkSet_LinkClicked(null, null);
        }

        private void txtKey_DoubleClick(object sender, EventArgs e)
        {
            SetTag = !SetTag;

        }

        private void frmEditor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                lnkCancel_LinkClicked(null, null);
        }

    }
}
